﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using SRM;
using SRM.DataObjects;

public partial class UserControls_LanguageProficiencyUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    private bool ValidateFormFields(string mode)
    {
        bool hasFilledAllRequiredFields = true;

        if (mode == "UPDATE")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(LanguageProficiency_ObjectDataSource.UpdateParameters["language"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(LanguageProficiency_ObjectDataSource.UpdateParameters["ability"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Ability Level not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(LanguageProficiency_ObjectDataSource.UpdateParameters["proficiency"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        if (mode == "INSERT")
        {
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(LanguageProficiency_ObjectDataSource.InsertParameters["language"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(LanguageProficiency_ObjectDataSource.InsertParameters["ability"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Ability Level not filled
            if ((hasFilledAllRequiredFields) && (string.IsNullOrEmpty(LanguageProficiency_ObjectDataSource.InsertParameters["proficiency"].DefaultValue)))
                hasFilledAllRequiredFields = false; // Language Proficiency not filled
        }

        return hasFilledAllRequiredFields;
    }

    protected void rgLanguageProficiency_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            LanguageProficiency_ObjectDataSource.UpdateParameters["languageKey"].DefaultValue = SRM.Help.GetHiddenFieldValue(item.FindControl("languageKey"));
            LanguageProficiency_ObjectDataSource.UpdateParameters["language"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("language_combobox"));
            LanguageProficiency_ObjectDataSource.UpdateParameters["ability"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("ability_combobox"));
            LanguageProficiency_ObjectDataSource.UpdateParameters["proficiency"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("proficiency_combobox"));

            if (ValidateFormFields("UPDATE"))
            {
                LanguageProficiency_ObjectDataSource.Update();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("LanguageProficiencyMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
        }

        if (e.CommandName == RadGrid.DeleteCommandName)
        {
            GridDataItem item = e.Item as GridDataItem;
            LanguageProficiency_ObjectDataSource.DeleteParameters["languageKey"].DefaultValue = item["LanguageKey_column"].Text;
            LanguageProficiency_ObjectDataSource.Delete();
        }

        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            LanguageProficiency_ObjectDataSource.InsertParameters["language"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("language_combobox"));
            LanguageProficiency_ObjectDataSource.InsertParameters["ability"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("ability_combobox"));
            LanguageProficiency_ObjectDataSource.InsertParameters["proficiency"].DefaultValue = SRM.Help.GetRadComboBoxValue(item.FindControl("proficiency_combobox"));

            if (ValidateFormFields("INSERT"))
            {
                LanguageProficiency_ObjectDataSource.Insert();
            }
            else
            {
                e.Canceled = true;
                System.Web.UI.WebControls.Literal msg = item.FindControl("LanguageProficiencyMessage") as System.Web.UI.WebControls.Literal;
                if (msg != null)
                {
                    msg.Text = "<p class='warning'>Please fill in required fields.</p>";
                }
            }
            
        }
    } // protected void rgLanguageProficiency_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)

    protected void rgLanguageProficiency_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            DataTable dt = null;
            GridEditFormItem geiEditedItem = e.Item as GridEditFormItem;
            geiEditedItem.Visible = true;

            // Declare control variables
            Telerik.Web.UI.RadComboBox language_combobox = null;
            Telerik.Web.UI.RadComboBox ability_combobox = null;
            Telerik.Web.UI.RadComboBox proficiency_combobox = null;

            // Find and Assign control variables
            language_combobox = geiEditedItem.FindControl("language_combobox") as Telerik.Web.UI.RadComboBox;
            ability_combobox = geiEditedItem.FindControl("ability_combobox") as Telerik.Web.UI.RadComboBox;
            proficiency_combobox = geiEditedItem.FindControl("proficiency_combobox") as Telerik.Web.UI.RadComboBox;

            // Databind the combo-boxes (if any)
            Help.BindSapCodeToComboBox(language_combobox, SRM_Code.GetLanguage("1"));
            Help.BindSapCodeToComboBox(ability_combobox, SRM_Code.GetLanguageAbility("1"));
            Help.BindSapCodeToComboBox(proficiency_combobox, SRM_Code.GetLanguageProficiency("1"));

            // Assign values to control variables
            if ((e.Item.DataItem != null) && (e.Item.DataItem is DataRowView))
            {
                System.Data.DataRow row = ((DataRowView)e.Item.DataItem).Row;

                Help.SetComboBoxSelectedValue(language_combobox, row.Field<string>("Language"));
                Help.SetComboBoxSelectedValue(ability_combobox, row.Field<string>("Ability"));
                Help.SetComboBoxSelectedValue(proficiency_combobox, row.Field<string>("Proficiency"));

            }

        }
    }
}